import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout'
import useUserStore from '@/store/modules/user'

// 角色路由分配开始
// 老师端路由
export const teachRoutes = [
  {
    path: '/index',
    component: () => import('@/views/teacher/welcome'),
    name: 'Index',
    meta: { title: '首页', icon: 'index', affix: true, activeMenu: "/index" },
    hidden: false
  },
  {
    path: '/class',
    name: 'class',
    redirect: "/userlist",
    component: () => import('@/views/teacher/class'),
    meta: { title: '班级管理', icon: 'class', activeMenu: "/class" },
    hidden: false,
    children: [
      {
        path: '/userlist',
        name: 'userlist',
        component: () => import('@/views/teacher/class/children/Userlist'),
        hidden: true
      },
      {
        path: '/applylist',
        name: 'applylist',
        component: () => import('@/views/teacher/class/children/Applylist'),
        meta: { title: '申请列表' },
        hidden: true
      },
      {
        path: '/join',
        name: 'join',
        component: () => import('@/views/teacher/class/children/Joinus'),
        meta: { title: '邀请加入' },
        hidden: true
      },
      {
        path: 'student',
        name: 'student',
        component: () => import('@/views/admin/system/children/Student'),
        meta: { title: '学生详情' },
        hidden: true
      },

    ]
  },
  {
    path: '/classlesson',
    name: 'classlesson',
    redirect: "/classlesson/classlist",
    component: () => import('@/views/teacher/classlesson'),
    meta: { title: '班课管理', icon: 'classlesson', activeMenu: "/classlesson" },
    hidden: false,
    children: [
      {
        path: "/classlesson/classlist",
        name: 'classlist',
        component: () => import('@/views/teacher/classlesson/ClassList'),
        hidden: true
      }
    ]
  },


  {
    path: '/designclass',
    name: 'designclass',
    redirect: "/designclass/standard",
    component: () => import('@/views/teacher/designclass'),
    meta: { icon: 'classlesson', activeMenu: "/classlesson" },
    hidden: true,
    children: [

      {
        path: "standard",
        name: "standard",
        component: () => import('@/views/teacher/designclass/children/Standard'),
        meta: { title: '课程标准' },

      },

      {
        path: "teachplan",
        name: "teachplan",
        component: () => import('@/views/teacher/designclass/children/TeachPlan'),
        meta: { title: '授课计划' },

      },

      {
        path: "syllabus",
        name: "syllabus",
        component: () => import('@/views/teacher/designclass/children/Syllabus'),
        meta: { title: '教学大纲' },

      },
      {
        path: "material",
        name: "material",
        component: () => import('@/views/teacher/designclass/children/Material'),
        meta: { title: '资料素材' },

      },

      {
        path: "works",
        name: "works",
        component: () => import('@/views/teacher/designclass/children/Works'),
        meta: { title: '作业管理' },

      },
      {
        path: "referwork",
        name: "referwork",
        component: () => import('@/views/teacher/designclass/children/ReferWork'),
        meta: { title: '查阅作业' },

      },

      {
        path: "wkdetail",
        name: "wkdetail",
        component: () => import('@/views/teacher/designclass/children/WkDetail'),
        meta: { title: '作业详情' },
        // hidden: true
      },

      // {
      //   path:"sign",
      //   name:"sign",
      //   component:()=>import('@/views/teacher/classlesson/designclass/children/Sign'),
      //   meta: { title: '签到管理' },
      //   hidden: true
      // }
    ]
  },

  {
    path: '/resource',
    name: 'resource',
    component: () => import('@/views/teacher/resource'),
    meta: { title: '资源库', icon: 'excel' ,activeMenu:"/resource"},
    hidden: false
  }

  // {
  //   path: '/resource',
  //   name: 'resource',
  //   component: () => import('@/views/teacher/resource'),
  //   meta: { title: '资源库', icon: 'excel' ,activeMenu:"/resource"},
  //   hidden: false
  // },
  // {
  //   path: '/cloudcourse',
  //   name: 'cloudcourse',
  //   component: () => import('@/views/teacher/cloudcourse'),
  //   meta: { title: '在线云课堂', icon: 'cloud',activeMenu:"/cloudcourse" },
  //   hidden: false
  // },
  // {
  //   path: '/aimodel',
  //   name: 'aimodel',
  //   component: () => import('@/views/teacher/aimodel'),
  //   meta: { title: 'AIGC模型', icon: 'ai',activeMenu:"/aimodel" },
  //   hidden: false
  // }
]

// 管理端路由
export const adminRoutes = [
  {
    path: '/index',
    component: () => import('@/views/admin/welcome'),
    name: 'Index',
    meta: { title: '首页', icon: 'index', affix: true, activeMenu: "/index" },
    hidden: false
  },
  {
    path: '/tenant',
    name: 'tenant',
    component: () => import('@/views/admin/tenant'),
    meta: { 
        title: '租户管理', 
        icon: 'database-fill',
        activeMenu: "/tenant"
    },
    hidden: false
  },
  {
    path: '/system',
    component: () => import('@/views/admin/system'),

    name: 'system',
    meta: { title: '系统管理', icon: 'system', activeMenu: "/system" },
    hidden: false,

    children: [
      {
        path: 'userlist',
        component: () => import('@/views/admin/system/children/UserList'),
        name: 'userlist',
        meta: { title: '用户管理', icon: 'user', activeMenu: "/system/userlist" },
        hidden: false,
      },
      // {
      //   path: 'applylist',
      //   component: () => import('@/views/admin/system/children/ApplyList'),
      //   name: 'applylist',
      //   meta: { title: '申请人列表', icon: '',activeMenu:"/system/applylist"},
      //   hidden: false,
      // },

      // {
      //   path: 'roleslist',
      //   component: () => import('@/views/admin/system/children/RolesList'),
      //   name: 'roleslist',
      //   meta: { title: '角色管理', icon: '',activeMenu:"/system/roleslist"},
      //   hidden: false,
      // },

      {
        path: 'majorlist',
        component: () => import('@/views/admin/system/children/MajorList'),
        name: 'majorlist',
        meta: { title: '专业方向管理', icon: 'documentation', activeMenu: "/system/majorlist" },
        hidden: false,
      },
      // {
      //   path: 'join',
      //   name: 'join',
      //   component: () => import('@/views/admin/system/children/JoinUs'),
      //   meta: { title: '邀请加入',icon: '',activeMenu:"/system/userlist" },
      //   hidden: true
      // },
      {
        path: 'student',
        name: 'student',
        component: () => import('@/views/admin/system/children/Student'),
        meta: { title: '学生详情' },
        hidden: true
      },

    ]
  },
  {
    path: '/classlesson',
    name: 'classlesson',
    redirect: "/classlesson/classlist",
    component: () => import('@/views/admin/classlesson'),
    meta: { title: '班课管理', icon: 'classlesson', activeMenu: "/classlesson" },
    hidden: false,
    children: [
      {
        path: "/classlesson/classlist",
        name: 'classlist',
        component: () => import('@/views/admin/classlesson/ClassList'),
        hidden: true
      }
      
    ]
  },

  {
    path: '/designclass',
    name: 'designclass',
    redirect: "/designclass/standard",
    component: () => import('@/views/admin/designclass'),
    meta: { icon: 'classlesson', activeMenu: "/classlesson" },
    hidden: true,
    children: [

      {
        path: "standard",
        name: "standard",
        component: () => import('@/views/admin/designclass/children/Standard'),
        meta: { title: '课程标准' },
        hidden: true
      },

      {
        path: "teachplan",
        name: "teachplan",
        component: () => import('@/views/admin/designclass/children/TeachPlan.vue'),
        meta: { title: '授课计划' },
        hidden: true
      },

      {
        path: "syllabus",
        name: "syllabus",
        component: () => import('@/views/admin/designclass/children/Syllabus'),
        meta: { title: '教学大纲' },
        hidden: true
      },
      {
        path: "material",
        name: "material",
        component: () => import('@/views/admin/designclass/children/Material'),
        meta: { title: '资料素材' },
        hidden: true
      },

      {
        path: "works",
        name: "works",
        component: () => import('@/views/admin/designclass/children/Works'),
        meta: { title: '作业管理' },
        hidden: true
      },
      {
        path: "referwork",
        name: "referwork",
        component: () => import('@/views/admin/designclass/children/ReferWork'),
        meta: { title: '查阅作业' },
        hidden: true
      },

      {
        path: "wkdetail",
        name: "wkdetail",
        component: () => import('@/views/admin/designclass/children/WkDetail'),
        meta: { title: '作业详情' },
        hidden: true
      },

      // {
      //   path:"sign",
      //   name:"sign",
      //   component:()=>import('@/views/teacher/classlesson/designclass/children/Sign'),
      //   meta: { title: '签到管理' },
      //   hidden: true
      // }
    ]
  },

  {
    path: '/resource',
    name: 'resource',
    component: () => import('@/views/teacher/resource'),
    meta: { title: '资源库', icon: 'excel' ,activeMenu:"/resource"},
    hidden: false
  },
  {
    path: '/tenant',
    name: 'tenant',
    component: () => import('@/views/admin/tenant'),
    meta: { 
        title: '租户管理', 
        icon: 'database-fill',
        activeMenu: "/tenant",
        requireSuper: true
    },
    hidden: true
  }
]

//学生端路由
export const studentRoutes = [
  {
    path: '/index',
    component: () => import('@/views/student/Home'),
    name: 'Index',
    meta: { title: '首页', icon: 'index', affix: true, activeMenu: "/index" },
    hidden: false
  },
  {
    path: '/classlesson',
    name: 'classlesson',
    redirect: "/classlesson/classlist",
    component: () => import('@/views/student/classlesson'),
    meta: { title: '我的班课', icon: 'classlesson', activeMenu: "/classlesson" },
    hidden: false,
    children: [
      {
        path: "/classlesson/classlist",
        name: 'classlist',
        component: () => import('@/views/student/classlesson/ClassList'),
        hidden: true
      } 
    ]
  },

  {
    path: '/designclass',
    name: 'designclass',
    redirect: "/designclass/syllabus",
    component: () => import('@/views/student/designclass'),
    meta: { icon: 'classlesson', activeMenu: "/classlesson" },
    hidden: true,
    children: [

      {
        path: "syllabus",
        name: "syllabus",
        component: () => import('@/views/student/designclass/children/Syllabus'),
        meta: { title: '大纲' },
        hidden: true
      },

      {
        path: "works",
        name: "works",
        component: () => import('@/views/student/designclass/children/Works'),
        meta: { title: '作业' },
        hidden: true
      },

      {
        path: "wkdetail",
        name: "wkdetail",
        component: () => import('@/views/student/designclass/children/WkDetail'),
        meta: { title: '作业详情' },
        hidden: true
      }


    ]
  },
]
// 根据登陆成功后端返回的角色判断执行哪个路由
export function dynamicRoutes(roles) {
    let routes = []
    if (roles === "老师组") {
        routes = teachRoutes
    } else if (roles === "管理组") {
        const userStore = useUserStore()
        // 如果是超级管理员，保留租户管理路由
        routes = adminRoutes.filter(route => {
            if (route.path === '/tenant') {
                return userStore.is_super
            }
            return true
        })
    } else if (roles === "学生组") {
        routes = studentRoutes
    }
    
    return routes
}

// 公共路由
export const constantRoutes = [
  {
    path: '/user',
    component: Layout,
    redirect: '/userinfo',
    children: [
      {
        path: "/userinfo",
        component: () => import('@/views/user/Userinfo'),
        name: 'userinfo',
        meta: { title: '个人中心', icon: 'user' },
        redirect: '/userinfo/basicinfo',
        children: [
          {
            path: "basicinfo",
            component: () => import('@/views/user/children/BasicInfo.vue'),
            name: 'basicinfo',
            meta: { title: '基本信息', icon: 'user' },
          },

          {
            path: "personal",
            component: () => import('@/views/user/children/Personal.vue'),
            name: 'personal',
            meta: { title: '个人资料', icon: 'user' },
          },
          {
            path: "password",
            component: () => import('@/views/user/children/Passward.vue'),
            name: 'passward',
            meta: { title: '修改密码', icon: 'user' },
          }

        ]

      }
    ]

  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login')
  },

  {
    path: '/cglogin',
    name: 'cglogin',
    component: () => import('@/views/CgLogin')
  },

  // 大纲预览
  {
    path: '/chapter',
    component: () => import('@/views/chapter/index.vue'),
    name: 'course',
  },

]





const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
})

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    
    // 如果访问租户管理页面，检查是否是超级管理员
    if (to.path === '/tenant' && !userStore.is_super) {
        next('/404')
        return
    }
    
    next()
})

export default router
